package com.ls.nexgo;

import android.content.Context;

import androidx.annotation.NonNull;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;

import com.nexgo.oaf.apiv3.device.led.LightModeEnum;
import com.nexgo.oaf.apiv3.device.printer.AlignEnum;

import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;

public class NexgoPrinterPlugin implements FlutterPlugin, MethodCallHandler {
    private Context context;
    private MethodChannel channel;
    private final String TAG = "NexGo";
    private PrintNexgo printNexgo;

    @Override
    public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBinding) {
        context = flutterPluginBinding.getApplicationContext();
        channel = new MethodChannel(flutterPluginBinding.getBinaryMessenger(), "printer");
        channel.setMethodCallHandler(this);
    }

    @Override
    public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
        if (call.arguments != null) {
            Log.d("arguments", call.arguments.toString());
        }
        switch (call.method) {
            case ChangnelConfig.PRINT_INIT:
                try {
                    if (printNexgo == null) {
                        printNexgo = new PrintNexgo(context);
                    }
                    result.success(true);
                } catch (Exception e) {
                    Log.e(call.method, e.toString());
                    result.success(false);
                }
                break;
            case ChangnelConfig.PRINT_TEXT:
                String text = call.argument("text");
                onPrintText(text, AlignEnum.valueOf(call.argument("align")));
                result.success(true);
                break;
            case ChangnelConfig.PRINT_FEED:
                if (call.argument("feed") instanceof Integer) {
                    Integer feed = call.argument("feed"); // 拆箱
                    onPrintFeed(feed);
                    result.success(true);
                } else {
                    result.success(false);
                }
                break;
            case ChangnelConfig.PRINT_BMP:
                try {
                    byte[] imageData = call.argument("image");
                    if (imageData != null) {
                        Bitmap bitmap = BitmapFactory.decodeByteArray(imageData, 0, imageData.length);
                        onPrintImage(bitmap, AlignEnum.valueOf(call.argument("align")));
                        result.success(true);
                    } else {
                        result.success(false);
                    }
                } catch (Exception e) {
                    Log.e(call.method, e.toString());
                    result.success(false);
                }
                break;
            case ChangnelConfig.PRINT_LED:
                try {
                    LightModeEnum light = LightModeEnum.valueOf(call.argument("light"));
                    Boolean enabled = call.argument("enabled");
                    if (enabled == null) {
                        enabled = false;
                    }
                    onLED(light, enabled);
                    result.success(true);
                } catch (Exception e) {
                    result.success(false);
                }
                break;
            default:
                result.success(false);
                break;
        }
    }

    @Override
    public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
        channel.setMethodCallHandler(null);
    }

    void onPrintText(String text, AlignEnum align) {
        printNexgo.setText(text, align, context);
    }

    void onPrintFeed(Integer feed) {
        printNexgo.setFeed(feed * 10, context);
    }

    void onPrintImage(Bitmap image, AlignEnum align) {
        printNexgo.setImage(image, align, context);
    }

    void onLED(LightModeEnum light, Boolean enabled) {
        printNexgo.setLED(light, enabled);
    }
}
